# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=grpc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.34.0
_abseil_ver=20200923.2
pkgrel=1
pkgdesc="gRPC - Google's high performance, open source, general RPC framework (mingw-w64)"
arch=('any')
url='https://www.grpc.io/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf>=3.5.0"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-re2"
             "automake" "autoconf")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/grpc/grpc/archive/v${pkgver}.tar.gz"
        "abseil-cpp-${_abseil_ver}.tar.gz::https://github.com/abseil/abseil-cpp/archive/${_abseil_ver}.tar.gz"
        "0001-abseil-Fix-compiler-warnings.patch"
        "0002-abseil-Remove-librt-library.patch")
sha256sums=('7372a881122cd85a7224435a1d58bc5e11c88d4fb98a64b83f36f3d1c2f16d39'
            'bf3f13b13a0095d926b25640e060f7e13881bd8a792705dd9e161f3c2b9aa976'
            '9a8d939b5a0978a4b6658aa9061423111ede3d33a330e654e4a977b1eda63c92'
            'd3ecabcebf64c2211b40e07f376de028ad1b101051cba7e69d77f6074217238d')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd "${srcdir}/${_realname}-${pkgver}"
  #patch -p1 -i ${srcdir}/001-fix-linking.patch
  #patch -Np1 -i ${srcdir}/002-fix-verify-_WIN32_WINNT.patch

  cd ${srcdir}

  rmdir ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp
  mv abseil-cpp-${_abseil_ver} ${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp

  # https://github.com/abseil/abseil-cpp/pull/790
  cd "${srcdir}/${_realname}-${pkgver}/third_party/abseil-cpp"
  patch -Np1 -i "${srcdir}/0001-abseil-Fix-compiler-warnings.patch"
  patch -Np1 -i "${srcdir}/0002-abseil-Remove-librt-library.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  # remove __USE_MINGW_ANSI_STDIO=1 define, it breaks grpc string handling for
  # cases like 'gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR ...'
  CPPFLAGS=${CPPFLAGS//-D__USE_MINGW_ANSI_STDIO=1/}

  case "${CARCH}" in
    x86_64)
      LDFLAGS+=" -Wl,--disable-dynamicbase,--default-image-base-low"
    ;;
  esac

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCMAKE_SKIP_RPATH="ON" \
      -DBUILD_SHARED_LIBS="ON" \
      -DgRPC_ZLIB_PROVIDER="package" \
      -DgRPC_CARES_PROVIDER="package" \
      -DgRPC_PROTOBUF_PROVIDER="package" \
      -DgRPC_RE2_PROVIDER="package" \
      -DgRPC_SSL_PROVIDER="package" \
      -DgRPC_BENCHMARK_PROVIDER="package" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  #make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
